<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*人脸平板Api*/
class FaceTabletsApi extends  BaseApi
{
	private $model;
    private $AppKey = ['121420177'];     // key
	
	/**
	 * 测试地址
	 * */
	 public function _initialize()
    {
    }
	//过滤器
   	private function _before_method(){
//	  		if(request()->isPost()){
//	  			return $this->ajax_error("","","非法访问！");
//	  		} 	
  			$token=input("token/s","");
			$time=input("time/d","");
			$shop_id=input("shop_id/d","");
			$addr=input("addr","");
			$sign=input("sign","");
			
			$shopStatus=cache("ShopStatus_".$shop_id);
			if($shopStatus===FALSE){
				cache("ShopStatus_".$shop_id,db("Shop")->where("id=".$shop_id)->value("status"));
				$shopStatus=cache("ShopStatus_".$shop_id);
			}
			if(empty($shopStatus)){
				$this->ajax_error('','','您的场馆账号异常！请和管理员联系');
			}
				
			if(strlen($token)<5||strlen($time)<5||strlen($addr)<1||empty($shop_id)||empty($sign)){
				return $this->ajax_error("","","参数错误");
			}
			if(input("debug")==1){
			}else{
				if(($time+300)<time()||($time-300)>time()){//接口调用时间操过5分钟
					return $this->ajax_error("","","链接已经过期");
				}
				if($sign!=$this->_get_sign($token, $time)){
					return $this->ajax_error("","","签名错误！");
				}
			}
   	}
	/*获取会员列表*/
	function GetMemberListByMobile(){
		$this->_before_method();
		$shop_id=input("shop_id/d","0");
		$mobile=input("mobile/s","");
		if(strlen($mobile)!=4){
			return $this->ajax_error("","","手机号格式错误");
		}
		$mobiles=db("Interface")->alias("a")->join([["m_gym_order b","a.card_no=b.card_no","LEFT"]])->where("a.shop_id=$shop_id and substring(a.mobile,8,4)='$mobile'")->fetchsql(FALSE)->field("a.owner_type,a.card_no,a.mobile,a.name,b.gym_card_name")->select();
		foreach ($mobiles as $key => $value) {
			$mobiles[$key]['mobile']=preg_replace("/(\d{3})\d\d(\d{2})/", "\$1****\$3", $value['mobile']);
			if(empty($value['gym_card_name'])){
				if($value['owner_type']==1){
					$mobiles[$key]['gym_card_name']="会员卡";
				}else if($value['owner_type']==2){
					$mobiles[$key]['gym_card_name']="员工卡";
				}else if($value['owner_type']==3){
					$mobiles[$key]['gym_card_name']="临时卡";
				}else if($value['owner_type']==5){
					$mobiles[$key]['gym_card_name']="副卡";
				}else{
					$mobiles[$key]['gym_card_name']="会员卡";
				}
			}
			//过滤没有人脸模板的卡号
			$card_no=db("FaceCode")->where("shop_id=$shop_id and card_no='".$value['card_no']."'")->column("card_no");
			if(empty($card_no)){//没有人脸模板
				unset($mobiles[$key]);
			}
		}
		if(empty($mobiles)){
			return $this->ajax_error("","","未查询到结果或未录入人脸模板");
		}else{
			return $this->ajax_success(array_values($mobiles));
		}
	}
//	/*获取会员人脸特征*/
	function GetMemberFaceCode(){
		$this->_before_method();
		$shop_id=input("shop_id/d","0");
		$card_no=input("card_no/s","");
		if(strlen($card_no)<3){
			return $this->ajax_error("","","卡号格式错误");
		}
		$datas=db("FaceCode")->where("shop_id='$shop_id' and card_no='$card_no'")->value("face1");
		if(empty($datas)){
			return $this->ajax_error("","","未录入模板");
		}else{
			return $this->ajax_success($datas,"","成功","");
		}
	}
	/*入场*/
	function SwipeCard(){
		$this->_before_method();
		$card_no=input("card_no/s","");
		if(strlen($card_no)<3||empty(input("brakemachine_no"))||empty(input("has_txt"))){
			return $this->ajax_error("","","卡号格式错误");
		}
		$behavior_type=input("behavior_type",1);//行为方式 1入场 2出场
		$out_varify=input("out_varify",1);// 出场是否   1不验证  2验证
		if($behavior_type==1){//入场
			action("OcmApi/InSwipeCard");	
		}elseif($behavior_type==2){//出场
		    if($out_varify==1){
		    	$this->OutSwipeCard();	
		    }else{
		    	$this->_GetFaces();	
		    }
		}
	}
	/*统计会员卡数据*/
	function GetCardInfo(){
		$this->_before_method();
		$card_no=input("card_no");
		$shop_id=input("shop_id");
		$interfaces=action("api/Interfaces/get_old_interface",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		if(empty($interfaces)){
			return $this->ajax_error("","","卡号格式错误或参数错误");
		}
		$card_no=$interfaces['card_no'];// 原卡号
	    $gymorder=db("GymOrder")->where("card_no='$card_no' and shop_id=$shop_id and is_delete=0")->find();
		$contractInfo=action("api/OcmApi/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id,"mobile"=>""]);
		$contractInfo=$contractInfo['data'];
		$datas['name']=$interfaces['name'];
		$datas['able_day']=0;
		if(empty($gymorder)){
				$datas['start_date']="无";
				$datas['end_date']="无";
				if($interfaces['owner_type']==2){
					$datas['gym_card_name']="员工卡";	
					$datas['gym_card_type']="员工卡";
					$datas['type']=2;
					$datas['name']=$interfaces['name'];
				}else if($interfaces['owner_type']==3){
					$datas['gym_card_name']="临时卡";
					$datas['gym_card_type']="临时卡";	
					$datas['type']=3;
					$datas['name']="临时卡";
				}else if($interfaces['owner_type']==5){
					$datas['gym_card_name']="副卡";
					$datas['gym_card_type']="副卡";	
					$datas['type']=5;
				}
				else{
					$datas['type']=1;
					$datas['gym_card_name']="会员卡";
					$datas['gym_card_type']="会员卡";
					$startDate=isset($contractInfo['StartDate'])?$contractInfo['StartDate']:date("Y-m-d");
					$endDate=isset($contractInfo['EndDate'])?$contractInfo['EndDate']:date("Y-m-d");
					$datas['able_day']=$this->_get_able_day($startDate,$endDate);
				}
				$datas['able_count']=0;
				$datas['able_money']=0;
		}else{
			$datas['start_date']=$contractInfo['StartDate'];
			$datas['end_date']=$contractInfo['EndDate'];
			$datas['gym_card_name']="无";
			$datas['gym_card_type']=$contractInfo['MembershipType'];
			$datas['able_count']=$contractInfo['SurplusNum'];
			$datas['able_money']=0;
			$datas['able_day']=$this->_get_able_day($contractInfo['StartDate'],$contractInfo['EndDate']);
			$datas['type']=1;
		}
		return $this->api_ajax_success($datas);
	}
	function _get_able_day($start,$end){
	   if(strcmp($start,$end)<0){
	   	   if(strcmp(date("Y-m-d"),$start)<0){//未开卡
	   	   		$day=(strtotime("$end")-strtotime("$start"))/(24*60*60); 
	   	   }else{
	   	   	   $day=(strtotime("$end")-strtotime(date("Y-m-d")))/(24*60*60);
	   	   }
	   }else{
	   	  $day=0;
	   }	
	   return $day;
	}
	/*出场*/
	function OutSwipeCard(){
		$card_no=input("card_no/s","");
		if(strlen($card_no)<3||empty(input("brakemachine_no"))){
			return $this->ajax_error("","","卡号格式错误或者参数");
		}
		action("OcmApi/OutSwipeCard");
	}
	//回调
	function SwipeFingerPrintResult(){
		$this->_before_method();
		$card_no=input("card_no/s","");
		if(strlen($card_no)<3||empty(input("brakemachine_no"))){
			return $this->ajax_error("","卡号格式错误或参数错误");
		}
		$behavior_type=input("behavior_type",1);
		if($behavior_type==1){//入场
			action("OcmApi/SwipeFingerPrintResult");	
		}elseif($behavior_type==2){//出场		
			action("OcmApi/OutSwipeCard");
		}
		
	}
	//获取店铺信息
	function ShopInfo(){
	  $this->_before_method();
	  $shop_name=db("Shop")->where("id=".input("shop_id"))->value("shop_name");
	  $brakemachine_no=input("brakemachine_no","01");
	  if(empty($shop_name)){
	     return $this->api_ajax_error("","无效的店铺id");
	  }else{
	  	//再场人数
	  	$member_in=db("BehaviorLog")->where("shop_id=".input("shop_id")." and DATE_FORMAT(create_time,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d') and  (in_brakemachine_no in ('$brakemachine_no') or out_brakemachine_no in ('$brakemachine_no') )")->count(1);
		return $this->api_ajax_success(array("shop_name"=>$shop_name,"in_count"=>$member_in));
	  }
	}
	
	/*获取人脸模板*/
	function _GetFaces(){
		$card_no=input("card_no/s","");
		if(empty($card_no)||empty(input("brakemachine_no"))){
			return $this->ajax_error("","","参数错误",false);
		}
		//原卡号信息
		$interface=action("Interfaces/get_old_interface",['card_no'=>$card_no,'shop_id'=>input("shop_id")]);
		if(in_array($interface['owner_type'], ['1','2','5'])){//会员卡,员工卡,副卡
		   if($interface['in_varifycation']==1&&$interface['in_varifycation_type']==2){
		   		if(input("has_txt")=="21"){//本地有模板
			   	  	return $this->ajax_success("","","",false,2);	
			   	}
				$facecode=db("FaceCode")->where("card_no='".$interface['card_no']."' and shop_id=".input("shop_id"))->value("face1");	
			   	if(!empty($facecode)){
			   	  return $this->ajax_success($facecode,"","",false,"2");	
			   	  
			   	}		   	
		   }
		}else{//员工卡 临时卡
		}
		action("OcmApi/OutSwipeCard");
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * smartgym.com/api/FaceTabletsApi/test_GetMemberListByMobile
	 * */
	function test_GetMemberListByMobile(){
		$url="http://".$_SERVER['HTTP_HOST']."/api/FaceTabletsApi/GetMemberListByMobile";
		$time=time();
		$result= post_json($url, array("token"=>"121420177","time"=>$time,"shop_id"=>"7","addr"=>"ef:aa:cc","mobile"=>"4952","sign"=>$this->_get_sign("121420177", $time)));
		header('Content-Type:text/html; charset=utf-8');
		var_dump($result);die;
	}
	/**
	 * smartgym.com/api/FaceTabletsApi/test_GetMemberFaceCode
	 * */
	function test_GetMemberFaceCode(){
		$url="http://".$_SERVER['HTTP_HOST']."/api/FaceTabletsApi/GetMemberFaceCode";
		$time=time();
		$result= post_json($url, array("token"=>"121420177","time"=>$time,"shop_id"=>"7","addr"=>"ef:aa:cc","card_no"=>"0722843302","sign"=>$this->_get_sign("121420177", $time)));
		header('Content-Type:text/html; charset=utf-8');
		var_dump($result);die;
	}
	/**
	 * smartgym.com/api/FaceTabletsApi/test_InSwipeCard
	 * */
	function test_InSwipeCard(){
		$url="http://".$_SERVER['HTTP_HOST']."/api/FaceTabletsApi/InSwipeCard";
		$time=time();
		$result= post_json($url, array("token"=>"121420177","time"=>$time,"shop_id"=>"7",
										"addr"=>"ef:aa:cc","card_no"=>"0722843302",
										"sign"=>$this->_get_sign("121420177", $time),
										"brakemachine_no"=>"01",
										"cabinet_nos"=>"0",
										"has_txt"=>"20"
										));
		header('Content-Type:text/html; charset=utf-8');
		var_dump($result);die;
	}
	/**
	 * smartgym.com/api/FaceTabletsApi/test_OutSwipeCard
	 * */
	function test_OutSwipeCard(){
		$url="http://".$_SERVER['HTTP_HOST']."/api/FaceTabletsApi/OutSwipeCard";
		$time=time();
		$result= post_json($url, array("token"=>"121420177","time"=>$time,"shop_id"=>"7",
										"addr"=>"ef:aa:cc","card_no"=>"0722843302",
										"sign"=>$this->_get_sign("121420177", $time),
										"brakemachine_no"=>"01",
										"cabinet_nos"=>"0",
										"has_store"=>"0"
										));
		header('Content-Type:text/html; charset=utf-8');
		var_dump($result);die;
	}
	/**
	 * smartgym.com/api/FaceTabletsApi/test_SwipeFingerPrintResult
	 * */
	function test_SwipeFingerPrintResult(){
		$url="http://".$_SERVER['HTTP_HOST']."/api/FaceTabletsApi/SwipeFingerPrintResult";
		$time=time();
		$result= post_json($url, array("token"=>"121420177","time"=>$time,"shop_id"=>"7",
										"addr"=>"ef:aa:cc","card_no"=>"0722843302",
										"sign"=>$this->_get_sign("121420177", $time),
										"brakemachine_no"=>"01",
										"flag"=>"1"
										));
		header('Content-Type:text/html; charset=utf-8');
		var_dump($result);die;
	}
	/**
	 * smartgym.com/api/FaceTabletsApi/test_ShopInfo
	 * */
	function test_ShopInfo(){
		$url="http://".$_SERVER['HTTP_HOST']."/api/FaceTabletsApi/ShopInfo";
		$time=time();
		$result= post_json($url, array("token"=>"121420177","time"=>$time,"shop_id"=>"7",
										"addr"=>"ef:aa:cc",
										"sign"=>$this->_get_sign("121420177", $time),
										));
		header('Content-Type:text/html; charset=utf-8');
		var_dump($result);die;
	}
	/**
	 * 获取签名
	 * @param $key
	 * @param $timestamp
	 * */
	private function _get_sign($key,$timestamp){
		return md5($key."_".$timestamp);
	}
	
}
